package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping(path = "/getMenus.do")
    public Result getMenus(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //com.itheima.controller.UserController.getUsername()方法中已经判断过user是否为空，这里不再做判断
        final Collection<GrantedAuthority> authorities = user.getAuthorities();
        List<String> keywords=new ArrayList<>();
        //遍历取出ROLE开头的元素
        for (GrantedAuthority authority : authorities) {
            final String keyword = authority.getAuthority();
            if(keyword.startsWith("ROLE")){
                keywords.add(keyword);
            }
        }
        try {
            final List<Menu> allMenuList = menuService.findMenuByRoleKeywords(keywords);
            //返回的菜单集合同时包含父级菜单和子菜单，需要把子菜单放到父级菜单的children字段中
            List<Menu> parentMenuList=new ArrayList<>();
            //获取菜单最大Level
            int maxLevel=0;
            for (Menu menu : allMenuList) {
                if(menu.getLevel()>maxLevel){
                    maxLevel=menu.getLevel();
                }
            }
            //重新整理菜单集合
            reorganizeMenuList(allMenuList,parentMenuList,maxLevel,0);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,parentMenuList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    //为了使代码更具通用性，此处使用递归方法把子菜单对象加入到父菜单的children集合中
    public static void reorganizeMenuList(List<Menu> menuList,List<Menu> newList,Integer maxLevel,Integer currentLevel){
        currentLevel+=1;
        if(currentLevel<=maxLevel){
            if(currentLevel==1){
                for (Menu menu : menuList) {
                    if(menu.getLevel()==1){
                        newList.add(menu);
                    }
                }
                reorganizeMenuList(menuList,newList,maxLevel,currentLevel);
            }else {
                for (Menu parentMenu : newList) {
                    Integer parentId=parentMenu.getId();
                    for (Menu menu : menuList) {
                        if(menu.getParentMenuId()==parentId){
                            parentMenu.getChildren().add(menu);
                        }
                    }
                    reorganizeMenuList(menuList,parentMenu.getChildren(),maxLevel,currentLevel);
                }
            }
        }
    }
}
